package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.calendar.MonthDateView;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Statistics;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IHistoryReach;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.HistoryReachActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/7/10 14:50
 * 邮箱：zhanghuaiha@gmail.com
 */

public class HistoryReachPresenter extends BasePresenter<IHistoryReach> {

    private String versionCode;
    private String eid;
    private String udid;
    private String sid;
    private List<Integer> list;
    private HistoryReachActivity ac;
    private MonthDateView mdv;

    public HistoryReachPresenter(BaseActivity context) {
        super(context);
    }

    public void loadDate(String sid){
        versionCode = ObtainVersion.getVersionName(mContext);
        eid = App.SP.getString("id", "");
        udid = App.SP.getString("udid", "");
        this.sid = sid;
        list = new ArrayList<Integer>();
        ac = getView().getAC();
        mdv = getView().getMDV();

    }

    public void getDate(String start_date,String end_date,int type){
        String time = System.currentTimeMillis() + "";
        String encry = ObtainEncry.getStatistics_store("2", time, versionCode, NetWorkCodeInfo.STATISTICS_STORE, udid, eid, sid, start_date, end_date);
        RetrofitFactory.getmInstanse().mApi.getStatistics_store("2", time, versionCode, NetWorkCodeInfo.STATISTICS_STORE, udid, eid, sid,start_date,end_date,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<List<Statistics>>>() {
                    @Override
                    public void success(BaseResponseEntity<List<Statistics>> listBaseResponseEntity) {
                        if (listBaseResponseEntity.code != 200) {
                            ToastUtil.showToast(listBaseResponseEntity.message);
                        }else {
                            List<Statistics> data = listBaseResponseEntity.data;

//                            if (data.size() == 99) {
//                                ToastUtil.showToast("未发现当前月份参数");
//                            }else {
                                if (list.size() > 0) {
                                    list.clear();
                                }
                                for (int i = 0; i < data.size(); i++) {
                                    int time = 0;
                                    if (data.get(i).getIs_ok().equals("1")) {
                                        String date = data.get(i).getDate();

                                        time = Integer.parseInt(date.substring(date.length() - 2));

                                        list.add(time);
                                    }
                                }
                                if (list.size() == 0) {
                                    list.add(0);
                                }
                                mdv.setDaysHasThingList(list);
                                if (type == 1) {
                                    mdv.onLeftClick();
                                }else if (type == 2) {
                                    mdv.onRightClick();
                                }else{
                                    mdv.setTodayToView();
                                }

                            }
//                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                    }
                });
    }
}
